home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / awt / label~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  3.9 KB  |  160 lines

  1. /*
  2.  * @(#)Label.java    1.17 95/11/21 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.awt.peer.LabelPeer;
  22.  
  23. /**
  24.  * A component that displays a single line of read-only text.
  25.  *
  26.  * @version    1.17, 11/21/95
  27.  * @author     Sami Shaio
  28.  */
  29. public class Label extends Component {
  30.  
  31.     /**
  32.      * The left alignment.
  33.      */
  34.     public static final int LEFT     = 0;
  35.  
  36.     /** 
  37.      * The center alignment.
  38.      */
  39.     public static final int CENTER     = 1;
  40.  
  41.     /**
  42.      * The right alignment.
  43.      */
  44.     public static final int RIGHT     = 2;
  45.  
  46.     /**
  47.      * The label.
  48.      */
  49.     String label;
  50.     
  51.     /**
  52.      * The label's alignment.  The default alignment is set
  53.      * to be left justified.
  54.      */
  55.     int       alignment = LEFT;
  56.  
  57.     /**
  58.      * Constructs an empty label.
  59.      */
  60.     public Label() {
  61.     this("");
  62.     }
  63.  
  64.     /**
  65.      * Constructs a new label with the specified String of text.
  66.      * @param label the text that makes up the label
  67.      */
  68.     public Label(String label) {
  69.     this.label = label;
  70.     }
  71.  
  72.     /**
  73.      * Constructs a new label with the specified String of 
  74.      * text and the specified alignment.
  75.      * @param label the String that makes up the label
  76.      * @param alignment the alignment value
  77.      */
  78.     public Label(String label, int alignment) {
  79.     this.label = label;
  80.     setAlignment(alignment);
  81.     }
  82.  
  83.     /**
  84.      * Creates the peer for this label.  The peer allows us to
  85.      * modify the appearance of the label without changing its 
  86.      * functionality.
  87.      */
  88.     public synchronized void addNotify() {
  89.     peer = getToolkit().createLabel(this);
  90.     super.addNotify();
  91.     }
  92.  
  93.     /** 
  94.      * Gets the current alignment of this label. 
  95.      * @see #setAlignment
  96.      */
  97.     public int getAlignment() {
  98.     return alignment;
  99.     }
  100.  
  101.     /** 
  102.      * Sets the alignment for this label to the specified 
  103.      * alignment.
  104.      * @param alignment the alignment value
  105.      * @exception IllegalArgumentException If an improper alignment was given. 
  106.      * @see #getAlignment
  107.      */
  108.     public void setAlignment(int alignment) {
  109.     switch (alignment) {
  110.       case LEFT:
  111.       case CENTER:
  112.       case RIGHT:
  113.         this.alignment = alignment;
  114.         LabelPeer peer = (LabelPeer)this.peer;
  115.         if (peer != null) {
  116.         peer.setAlignment(alignment);
  117.         }
  118.         return;
  119.     }
  120.     throw new IllegalArgumentException("improper alignment: " + alignment);
  121.     }
  122.  
  123.     /** 
  124.      * Gets the text of this label. 
  125.      * @see #setText
  126.      */
  127.     public String getText() {
  128.     return label;
  129.     }
  130.  
  131.     /** 
  132.      * Sets the text for this label to the specified text.
  133.      * @param label the text that makes up the label 
  134.      * @see #getText
  135.      */
  136.     public void setText(String label) {
  137.     if (label != this.label && (this.label == null
  138.                     || !this.label.equals(label))) {
  139.         this.label = label;
  140.         LabelPeer peer = (LabelPeer)this.peer;
  141.         if (peer != null) {
  142.         peer.setText(label);
  143.         }
  144.     }
  145.     }
  146.  
  147.     /**
  148.      * Returns the parameter String of this label.
  149.      */
  150.     protected String paramString() {
  151.     String str = ",align=";
  152.     switch (alignment) {
  153.       case LEFT:   str += "left"; break;
  154.       case CENTER: str += "center"; break;
  155.       case RIGHT:  str += "right"; break;
  156.     }
  157.     return super.paramString() + str + ",label=" + label;
  158.     }
  159. }
  160.